package com.itheima.youdianda.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.domain.SysConfig;
import com.itheima.youdianda.dto.ConfigForm;
import com.itheima.youdianda.dto.ConfigRules;
import com.itheima.youdianda.dto.SysConfigGroupDto;
import com.itheima.youdianda.dto.SysConfigTagDto;
import com.itheima.youdianda.mapper.SysConfigMapper;
import com.itheima.youdianda.service.SysConfigService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author Pan Rundong
 * @Date 2023-02-13 20:56
 * @Desc 系统配置Service实现类
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {
    /**
     * 获取全部系统配置
     * @Author: Pan Rundong
     * @Date: 2023-02-13 21:28
     * @Greet2u:  （O｀ 3′O）~~♡
     * @return java.util.List<com.itheima.youdianda.dto.SysConfigTagDto>
     */
    @Override
    public List<SysConfigTagDto> getAll2() {
        //1.对config表排序
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SysConfig::getTabValue).orderByDesc(SysConfig::getGroupValue);
        List<SysConfig> sysConfigList = this.list(queryWrapper);

        //2.获取tag
        Set<String> set = new HashSet<>();
        sysConfigList.stream().forEach((item)->{
            set.add(item.getTabValue());
        });

        //3.获取tagdto
        List<SysConfigTagDto> tagDtoList = new ArrayList<>();
        for (String tag : set) {
            //设置tag名称
            SysConfigTagDto sysConfigTagDto = new SysConfigTagDto();
            sysConfigTagDto.setName(tag);
            sysConfigTagDto.setKey(tag);
            //搜寻对应分组
            List<SysConfigGroupDto> groupDtoList = getGroupList(tag);
            sysConfigTagDto.setChildren(groupDtoList);
            //添加入tagList
            tagDtoList.add(sysConfigTagDto);
        }
        return tagDtoList;
    }

    /**
     * 获取全部配置之alldata
     * @Author: Pan Rundong
     * @Date: 2023-02-15 0:32
     * @Greet2u:  （O｀ 3′O）~~♡
     * @return java.util.List<com.itheima.youdianda.domain.SysConfig>
     */
    @Override
    public List<SysConfig> getAllData() {
        //直接返还
        List<SysConfig> list = this.list();
        return list;
    }

    /**
     * 获取全部配置之configForm
     * @Author: Pan Rundong
     * @Date: 2023-02-15 0:36
     * @Greet2u:  （O｀ 3′O）~~♡
     * @return java.util.Map
     */
    @Override
    public ConfigForm getAttrValue() {
        //1.获取全部数据
        List<SysConfig> list = this.list();

        //2.遍历，以键值对存入map
        Map map = new HashMap<>(20);
        list.stream().forEach((item)->{
            map.put(item.getAttrKey(), item.getAttrName());
        });

        ConfigForm configForm = new ConfigForm(map);
        return configForm;
    }

    /**
     * 获取全部配置之rules
     * @Author: Pan Rundong
     * @Date: 2023-02-15 0:41
     * @Greet2u:  （O｀ 3′O）~~♡
     * @return java.lang.Object
     */
    @Override
    public Object getRules() {
        //设置条件过滤器
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getAttrKey, "site_name");
        SysConfig sysConfig = this.getOne(queryWrapper);
        //创建rules
        ConfigRules configRules = new ConfigRules();
        configRules.setSiteName(sysConfig.getValidator());
        return configRules;
    }

    /**
     * 获取对应tag的group
     * @Author: Pan Rundong
     * @Date: 2023-02-13 22:46
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param tag
     * @return java.util.List<com.itheima.youdianda.dto.SysConfigGroupDto>
     */
    private List<SysConfigGroupDto> getGroupList(String tag) {
        //1.初始化List
        List<SysConfigGroupDto> list = new ArrayList<>();
        //2.设置条件过滤器，获取对应tag配置
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getTabValue, tag);
        List<SysConfig> sysConfigList = this.list(queryWrapper);

        //3.获取对应group分组
        Set<String> set = new HashSet<>();
        sysConfigList.stream().forEach((item)->{
            set.add(item.getGroupValue());
        });

        //4.遍历，存入list
        for (String item : set) {
            SysConfigGroupDto sysConfigGroupDto = new SysConfigGroupDto();
            sysConfigGroupDto.setName(item);
            //4.1设置条件为group以及tag
            LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SysConfig::getGroupValue, item);
            lambdaQueryWrapper.eq(SysConfig::getTabValue, tag);
            List<SysConfig> sysConfigs = this.list(lambdaQueryWrapper);
            sysConfigGroupDto.setData(sysConfigs);
            //4.2加入list
            list.add(sysConfigGroupDto);
        }
        return list;
    }
}
